package JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import DAO.SalesDaoImpl;
import DBConn.DBConn;
import MySQL.Sales;;

public class salesAdd extends JFrame{
	private static final long serialVersionUID = -3798424987700534172L;
	JLabel e1,e2,e3,e4,msg;
	JTextField t1,t2,t3,t4;
	JButton b1;
	Connection conn;
	
	public salesAdd(){
	conn = DBConn.open();
	e1 = new JLabel("商品代码:");
	e2 = new JLabel("销售数量:");
	e3 = new JLabel("销售额:");
	e4 = new JLabel("销售日期:");
	t1 = new JTextField(10);
	t2 = new JTextField(10);
	t3 = new JTextField(10);
	t4 = new JTextField(10);
	msg = new JLabel("");
	b1 = new JButton("保存");

	//字体设置
	Font f = new Font("隶书",Font.BOLD,23);
	e1.setFont(f);t1.setFont(f);
	e2.setFont(f);t2.setFont(f);
	e3.setFont(f);t3.setFont(f);
	e4.setFont(f);t4.setFont(f);
	msg.setFont(f);
	b1.setFont(f);b1.setForeground(Color.blue);
	msg.setForeground(Color.lightGray);
	//位置设置
	e1.setBounds(30, 20,110,30); t1.setBounds(145, 20,200, 30);
	e2.setBounds(30, 80,110,30); t2.setBounds(145, 80,200, 30);
	e3.setBounds(40, 140,110,30); t3.setBounds(145, 140,200, 30);
	e4.setBounds(30, 200,110,30); t4.setBounds(145, 200,200, 30);
	b1.setBounds(150, 260,90,40); msg.setBounds(140, 300,120,30);
	
	this.add(e1);this.add(t1);
	this.add(e2);this.add(t2);
	this.add(e3);this.add(t3);
	this.add(e4);this.add(t4);
	this.add(msg);this.add(b1);
	//按钮：保存
	b1.addActionListener(new B1());
	
	this.setBounds(440,190,395,380);
	setTitle("添加进货单");
	this.setLayout(null); // 绝对布局
	setVisible(true);
	}
	
	//内部类：添加
	class B1 implements ActionListener
	{
		Sales s = new Sales();
		SalesDaoImpl sdi = new SalesDaoImpl();
		public void actionPerformed(ActionEvent e)
		{
			try{
				s.setId(t1.getText());
				s.setNumber(Integer.parseInt(t2.getText()));
				s.setPrice(Integer.parseInt(t3.getText()));
				s.setDate(Date.valueOf(t4.getText()));
				sdi.add(s);
				msg.setText("保存成功！");
			}catch (Exception e1) {
				msg.setText("嗯？请检查");
				t1.requestFocus();
			}finally{
				DBConn.close(conn);
			}
		}
	}
}
